package net.xiaosaguo.mymall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xiaosaguo.common.pojo.dto.MemberPrice;
import net.xiaosaguo.common.pojo.dto.SkuReductionDTO;
import net.xiaosaguo.common.utils.PageUtils;
import net.xiaosaguo.common.utils.Query;
import net.xiaosaguo.mymall.coupon.dao.SkuFullReductionDao;
import net.xiaosaguo.mymall.coupon.entity.MemberPriceEntity;
import net.xiaosaguo.mymall.coupon.entity.SkuFullReductionEntity;
import net.xiaosaguo.mymall.coupon.entity.SkuLadderEntity;
import net.xiaosaguo.mymall.coupon.service.MemberPriceService;
import net.xiaosaguo.mymall.coupon.service.SkuFullReductionService;
import net.xiaosaguo.mymall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;
    @Resource
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionDTO skuReductionDTO) {
        // sku 的优惠、满减等信息 sms_sku_ladder(打折)/sms_sku_full_reduction(满减)/sms_member_price(会员价)
        // 1. 商品阶梯价格 sms_sku_ladder(打折)
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionDTO.getSkuId());
        skuLadderEntity.setFullCount(skuReductionDTO.getFullCount());
        skuLadderEntity.setDiscount(skuReductionDTO.getDiscount());
        skuLadderEntity.setAddOther(skuReductionDTO.getCountStatus());
        if (skuReductionDTO.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        // 2. sms_sku_full_reduction(满减)
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionDTO, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            this.save(skuFullReductionEntity);
        }

        // 3. sms_member_price(会员价)
        List<MemberPrice> memberPriceList = skuReductionDTO.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntityList = memberPriceList
                .stream()
                .map(item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionDTO.getSkuId());
                    memberPriceEntity.setMemberLevelId(item.getId());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                })
                // 过滤会员价为 0 的信息
                .filter(memberPriceEntity -> memberPriceEntity.getMemberPrice().compareTo(new BigDecimal(0)) > 0)
                .collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntityList);
    }

}